package auto;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

import javax.swing.JOptionPane;

public class DodavanjeKorisnikaUBazu {

	private static Boolean ispravniPodaci(String prezime, String korisnicko,
			String pass, String maticni, String adresa, String telefon) {
		if (!masb.comp(prezime.compareTo("")))
			return false;
		if (!masb.comp(korisnicko.compareTo("")))
			return false;
		if (!masb.comp(pass.compareTo("")))
			return false;
		if (!masb.comp(maticni.compareTo("")))
			return false;
		if (!masb.comp(adresa.compareTo("")))
			return false;
		if (!masb.comp(telefon.compareTo("")))
			return false;
		return true;
	}

	public static void dodaj(String ime, String prezime, String korisnicko,
			String pass, String maticni, String adresa, String telefon,
			String telefon1, int ulogaID, boolean zaposlen, boolean aktivan,
			int ulogaSmena, String eMail) {
		String mySqlTxt;
		String dodatakPolja = "";
		try {
			if (ispravniPodaci(prezime, korisnicko, pass, maticni, adresa,
					telefon1) == true) {
				ulogaID += 1;
				Class.forName(masb.driverMySql);
				Connection conn = DriverManager.getConnection(masb.mySqlString);
				Statement st = conn.createStatement();
				if (ulogaSmena == 0)
					dodatakPolja = "";
				else
					dodatakPolja = ", Zaposlen, Aktivan";
				mySqlTxt = "INSERT INTO pKorisnik (Ime, Prezime, UlogaID, UserName, Pass, MaticniBR, Adresa, Telefon, Telefon2, DatumStart, Email"
						+ dodatakPolja
						+ ") VALUES ('"
						+ ime
						+ "','"
						+ prezime
						+ "',"
						+ ""
						+ ulogaID
						+ ",'"
						+ korisnicko
						+ "','"
						+ pass
						+ "','"
						+ maticni
						+ "','"
						+ adresa
						+ "','"
						+ telefon
						+ "','"
						+ telefon1
						+ "',now()"
						+ "','"
						+ eMail + "'";
				dodatakPolja = "";
				if (ulogaSmena == 0) {
					dodatakPolja += "," + masb.boolToInt(zaposlen) + ","
							+ masb.boolToInt(aktivan) + ")";
				} else
					dodatakPolja += ")";
				mySqlTxt += dodatakPolja;
				st.executeUpdate(mySqlTxt);
				conn.close();
				masb.msg("Uspesno dodavanje!");
			} else
				JOptionPane.showMessageDialog(null, "Lose popunjeni podaci");
		} catch (Exception e) {
			if (masb.admin)
				JOptionPane.showMessageDialog(null, e.getMessage());
		}
	}
}
